// JavaScript Document
 function today_add()
{
		var today_str=today_local();
		var today_start=today_str+" 00:00:01";
	var today_end=today_str+" 23:59:59";

	 var sql="select count(*)  cnt   from agtV4 where   timex>'$sdd$' and timex<'$end$'   "; 
   sql=sql.replace("$sdd$",today_start);
   sql=sql.replace("$end$",today_end);
   
   
			var orm=new atiOrmV5();
		orm.iniQbj();
		orm.obj=sql ;
	    orm.metaParam="retFmt=json";
		 
	orm.callbackFun=function(data)
	{
		
			try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				 
				  showErr(e);
				  return;
		  
		   }
		   var json=str2json(data);
		 	var obj=json[0];
			$("#today_agt_cnt").text(obj.cnt);
		  
		   
	};
	orm.query();
	
}

function btn_click()
{
	 var startDay=$("#txtBeginDate").val();
	 var endDay=$("#txtEndDate").val();
	var today_start=$("#txtBeginDate").val()+" 00:00:01";
	var today_end=$("#txtEndDate").val()+" 23:59:59";
	var where_s="";
	if(startDay.length>0)
		 where_s=" and `timex`>'"+today_start+"'";
	 if(endDay.length>0)
	 	 where_s=" and `timex`<'"+today_end+"'";
	var sql=" select count(*) cnt,parent_id,hiagt_nick as nick from agtV4 where 1=1   "+where_s+"  GROUP BY parent_id   ";
//	if($("#txtBeginDate").val()!="")
//		   sql=sql.replace("$sdd$",today_start);
//	   if($("#txtEndDate").val()!="")
// 		  sql=sql.replace("$end$",today_end);
	//if(urole=="admin")
	//	sql=" select sum(rmb) sumx from acc_rechg  where status=1 and  (promoter is null or promoter='')   ";
		
	sql=sql.replace("$exp$",where_s);
	console.log("my mem totle:"+sql);
		var orm=new atiOrmV5();
		orm.iniQbj();
		orm.obj=sql ;
	    orm.metaParam="retFmt=json";
		orm.notloginEventHandler=function(data)
		{
			  if(confirm("没有登录,请先登录。。"))
				  {					
					 window.location="../login/login.htm";
					 return;
				  }
		}
	orm.callbackFun=function(data)
	{
		
			try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				 
				  showErr(e);
				  return;
		  
		   }
		   var json=str2json(data);
		   // goodsVue.dodos=json;
		   // listQbf=json;
			goodsVue.$data.todos=json;
		//  new Vue({
//          el: '#ul-lists',
//          data: {
//            todos:listQbf   // [{nick:"xx"}]
//          }
//        });
		   
		   	
		   
	};
	orm.query();
	
}

function  page_load_finish()
{
	
	var v=parseInt($("#sumx_sub_agent_under_mem").text());
	var v2=parseInt($("#sumx").text());   //my under mem rechg total.
	if(  isNaN(v2))
	v2=0;
	$("#sumx_all").text( v+v2);
	
	//var commiss= calcCommission(v+v2);
	//$("#commiss").text(commiss);
}

var listQbf=[];
  var goodsVue ;
//  function fmtDate_local(timestamp)
//{
//	alert(timestamp);
//}
function page_load()
{
	
	
		
		
	//query my mem under my
	var sql="select * from agt_v order by id desc limit 100 ";
	sql="select count(*) cnt,parent_id,ac.nick from agtV3 a left join account ac on a.parent_id=ac.id GROUP BY parent_id";
	//if(urole=="admin")
	//	sql=" select sum(rmb) sumx from acc_rechg where promoter is null or promoter=''  ";
	var orm=new atiOrmV4();
		orm.obj=sql ;
		orm.metaParam="$utype=agent";
		orm.notloginEventHandler=function(data)
		{
			  if(confirm("没有登录,请先登录。。"))
				  {					
					 window.location="../login/login.htm";
					 return;
				  }
		}
	orm.callbackFun=function(data)
	{
		
			try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				 
				  showErr(e);
				  return;
		  
		   }
		   var json=str2json(data);
		  listQbf=json;
		  
	Vue.filter('timeFlt', function (value) {
        return fmtDate_local(value);
    })
	    goodsVue =  new Vue({
          el: '#ul-lists',
          data: {
            todos: json
          }
        });
		// goodsVue.todos=[{nick:"xx22"}]; jeig ok d 
		window.setTimeout(function(){
			console.log("---out");
			//goodsVue.$data.todos=[{nick:"xx22"}];
			//goodsVue.todos.$set(  [{nick:"xx22"}]); //bad
		},2000);
		
		// new Vue({
//          el: '#ul-lists',
//          data: {
//            todos: [{nick:"xx22"}]
//          }
//        });
		//	goodsVue.data.todos= [{nick:"xx22"}];
		//   var obj=json[0];
		   //var fm=new AForm()
		//   fm.bind(obj);
		   
		//   query_sumx_sub_agent_under_mem();
		   
		   	
		   
	};
	orm.query();
	
//	set_sub_total_a();
	
	
	
}

function set_sub_total_a()
{
	
	var s=$("#sub_total_a").attr("href");
	 
	 var code="new(com.attilax.token.TokenServiceV2).setUtype(agent).getuid()";
 
	   var jsb=new AtiJsBridge();
	
		jsb.exe("$code="+encodeURIComponent(code),function(data){
			console.log(data);
			 try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				  showErr(e);
				  return;
		  
		   }
		   if(urole=="admin")
		   		  s=s.replace("$pid$",0);
			 else
		   s=s.replace("$pid$",data);
		   $("#sub_total_a").attr("href",s);
		   
		   var s2=$("#my_mem_a").attr("href");
		      if(urole=="admin")
		   		  s2=s2.replace("$pid$",0);
			 else
				  s2=s2.replace("$pid$",data);
		   $("#my_mem_a").attr("href",s2);
		});
	 
	
}

function query_sumx_sub_agent_under_mem()
{
	 var code='new(com.attilax.agent.AgentRechargeService).getSubAgentTotal("pid:$pid$,startDate:$sd$,endDate:$ed$")';
	 
	  code=code.replace("$sd$",$("#txtBeginDate").val());
	   code=code.replace("$ed$",$("#txtEndDate").val());
	   
	 if(urole=="admin")
		  code=code.replace("$pid$",0);
	  else
		  code=code.replace("$pid$",uid_cur);
 
	   var jsb=new AtiJsBridge();
	
		jsb.exe("$code="+encodeURIComponent(code),function(data){
			console.log("query_sumx_sub_agent_under_mem ajax rzt:"+data);
			 try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				  showErr(e);
				  return;
		  
		   }
		   
		   $("#sumx_sub_agent_under_mem").text(data);
		     page_load_finish();
		  
		  	//  var json=str2json(data);
		  
			//  var list=new TableV3("table1");
		 
			//   list.bindData(json);
		  
		}); 
	
	return;	
	
	//---------------------------------------recomm follow
	var sql="select sum(rmb) as sumx_sub_agent_under_mem from recharge where accountId in (  select id as uid from account where promoter in ( select uid from  agent where  parent_id=$uid$ ) )";
	if(urole=="admin")
		 sql=sql.replace("$uid$",0);
	var orm=new atiOrmV4();
		orm.obj=sql ;
		orm.metaParam="$utype=agent";
	orm.callbackFun=function(data)
	{
		
			try{
			  processJavaEx_V2q315(data);
		   }catch(e)
		   {
			 
				 
				  showErr(e);
				  return;
		  
		   }
		   var json=str2json(data);
		  
		   var obj=json[0];
		   var fm=new AForm()
		   fm.bind(obj);
		   
		   page_load_finish();
		   
		   	
		   
	};
	//orm.query();
	
}
$(function()
{
	 today_add();
	
});